package com.erpanderp.wam.ac.model.service.reimbursement;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.FlushMode;
import org.hibernate.Query;
import org.hibernate.Session;

import com.erpanderp.wam.ac.model.entity.reimbursement.ProjectPerdiem;
import com.erpanderp.wam.ac.model.entity.reimbursement.ProjectPerdiemDetail;
import com.erpanderp.wam.util.HibernateUtil;

public class ProjectPerdiemDetailDAO {
	
	
	public boolean createProjectPerdiemDetails(ProjectPerdiemDetail projectPerdiemDetail){
		System.out.println("IN CREATE PROJECTPERDIEM ");
		System.out.println("PROJECTPERDIEM  TOTAL AMOUNT ");
		boolean saved = false;
		try{
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			
			session.beginTransaction();
			session.setFlushMode(FlushMode.COMMIT);
		    session.save(projectPerdiemDetail);
		    
		    session.getTransaction().commit();
		    saved = true;
		    return saved;
		}catch (Exception e) {
			e.printStackTrace();
			return false;
			// TODO: handle exception
		}
		
	}
	
	
	public ProjectPerdiemDetail getProjectPerdiemDetailsByProjectPerdiemIdAndRevision(Long projectPerdiemId,String revision ){
		System.out.println("PERDIEM DETAILS BY REVISION");
		System.out.println("PROJECT PERDIEM ID ="+projectPerdiemId+"REVISION ="+revision);
		ProjectPerdiemDetail projectPerdiemDetail = null;
		try{
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			session.setFlushMode(FlushMode.AUTO);
			String queryString = "from ProjectPerdiemDetail p where p.projectPerdiem.projectPerdiemId=:projectPerdiemId and p.revision=:revision";
			Query query = session.createQuery(queryString).setLong("projectPerdiemId", projectPerdiemId).setString("revision",revision);
			projectPerdiemDetail = (ProjectPerdiemDetail) query.uniqueResult();
			System.out.println("PROJECT PERDIEM DETAIL REVISION IS-------- "+projectPerdiemDetail.getRevision());
			session.getTransaction().commit();
			return projectPerdiemDetail;
		}catch (Exception e) {
			e.printStackTrace();
			return null;
			// TODO: handle exception
		}
		
	}
	
	public List<ProjectPerdiemDetail> getProjectPerdiemsDetailsByProjectPerdiemId(Long projectPerdiemId){
		System.out.println("PERDIEM DETAILS BY PROJECT PERDIEM ID");
		List<ProjectPerdiemDetail> projectPerdiemDetailsList = null;
		//System.out.println("PROJECT PERDIEM ID ="+projectPerdiemId+"REVISION ="+revision);
		ProjectPerdiemDetail projectPerdiemDetail = null;
		try{
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			session.setFlushMode(FlushMode.COMMIT);
			String queryString = "from ProjectPerdiemDetail p where p.projectPerdiem.projectPerdiemId=:projectPerdiemId";
			Query query = session.createQuery(queryString).setLong("projectPerdiemId", projectPerdiemId);
			projectPerdiemDetailsList = query.list();
			session.getTransaction().commit();
			return projectPerdiemDetailsList;
		}catch (Exception e) {
			e.printStackTrace();
			return null;
			// TODO: handle exception
		}
		
	}
	
	
	
	
	public static void main(String args[]){
		System.out.println("PROJECT "+new ProjectPerdiemDetailDAO().getProjectPerdiemDetailsByProjectPerdiemIdAndRevision(1L, "R3"));
	}
}
